package com.scisoft.scan.control.stockdistribute;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.control.CommonAdapter;
import com.scisoft.scan.control.CommonViewHolder;
import com.scisoft.scan.control.ScanHelper;
import com.scisoft.scan.data.BarCodeBean;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.stockdistribute.StockAreaBean;
import com.scisoft.scan.data.stockdistribute.StockAreaPositionBean;
import com.scisoft.scan.data.stockdistribute.StockDistributeBean;
import com.scisoft.scan.data.stockdistribute.StockPositionBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper;
import com.scisoft.scan.ws.WebServiceHelper_StockDistribute;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import static com.scisoft.scan.control.CommonAdapter.LOADING_END;
import static com.scisoft.scan.control.CommonAdapter.LOADING_MORE;
import static com.scisoft.scan.control.CommonAdapter.PULLUP_LOAD_MORE;

public class StockDistributeActivity extends Activity {

    private ImageView iv_Back; //返回按钮
    private ImageView iv_query; //搜索按钮
    private ClearEditText et_bar;//搜索框
    private List<StockAreaBean> areaBeanList = new ArrayList<>();
    private List<StockPositionBean> positionBeanList = new ArrayList<>();
    private String currentDepotId;
    //库区和货位
    private Spinner spin_area;
    private StockAreaBean currentAreaBean;
    private Spinner spin_position;
    private StockPositionBean currentPositionBean;
    //分页配置表
    private QueryPageBean pageBean = null;
    private int currentPage = 0;
    //分页显示控件
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private StockAdapter stockAdapter;
    private LinearLayoutManager mLayoutManager;
    private int lastVisibleItem; //最后一个可见Item的位置，关键所在
    float lastY = 0;
    private int scanFlag = 0;//货架扫码标识 0 扫描配件 1 扫描货架

    @Override
    @SuppressLint("ClickableViewAccessibility")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_stock_distribute);
        //初始化扫码
        init();
        scanFlag = 0;
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        et_bar = findViewById(R.id.et_bar);
        //搜索框
        iv_query = findViewById(R.id.iv_query);
        iv_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QueryData(true);
            }
        });
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        int fullWidth = displayMetrics.widthPixels;

        mRefreshLayout = findViewById(R.id.swipe_layout);
        //mRefreshLayout.setEnabled(false);
        mRefreshLayout.setProgressViewEndTarget(true, 0);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshLayout.setRefreshing(false);
                QueryData(true);
            }
        });
        mRecyclerView = findViewById(R.id.lv_data);
        stockAdapter = new StockAdapter(StockDistributeActivity.this);
        stockAdapter.setItemLayout(R.layout.item_stockdistribute, R.id.hsv, R.id.layout_show, R.id.layout_hide, fullWidth);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        stockAdapter.setOnItemClickListener(new StockAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //没有点击操作，只有左滑操作
            }
        });
        //滚动的时候清空左滑按钮
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (Math.abs(event.getY() - lastY) > 5)
                            stockAdapter.clearLeftMenuList();
                }
                return false;
            }
        });
        mRecyclerView.setAdapter(stockAdapter);
        //设置滑动监听
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //判断是否滑动到底
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == stockAdapter.getItemCount()) {
                    //滑动到底，需要改变状态为 上拉加载更多
                    stockAdapter.changeMoreStatus(LOADING_MORE);
                    QueryData(false);
                } else {
                    stockAdapter.changeMoreStatus(PULLUP_LOAD_MORE);
                }
            }

            //更新最后可见位置
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

            }
        });
        QueryData(true);
    }

    //查询库存
    private void QueryData(boolean refresh) {
        CommonUtil.showPress(this);
        QueryTask queryTask = new QueryTask(StockDistributeActivity.this, refresh);
        queryTask.execute();
    }

    //绑定数据
    private void BindData(Context context, List<StockDistributeBean> newData, boolean refresh) {
        //清空
        stockAdapter.clearLeftMenuList();
        //数据
        if (!refresh) {
            if (newData.size() > 0) {
                stockAdapter.addMoreItem(newData);
            } else {
                stockAdapter.changeMoreStatus(LOADING_END);
            }
        } else {
            if (newData.size() > 0) {
                stockAdapter.clearAddItem(newData);
            } else {
                stockAdapter.clearAddItem(newData);
                stockAdapter.changeMoreStatus(LOADING_END);
            }
        }
    }

    //绑定数据
    private void BindAreaData(final Context context) {
        List<String> areaList = new ArrayList<>();
        currentAreaBean = null;
        if (areaBeanList != null) {
            for (int i = 0; i < areaBeanList.size(); i++) {
                areaList.add(areaBeanList.get(i).areaName);
            }
            currentAreaBean = areaBeanList.get(0);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.style_spinner, areaList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_area.setAdapter(adapter);
        spin_area.setSelection(0, true);
        spin_area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentAreaBean = areaBeanList.get(position);
                CommonUtil.showPress(context);
                QueryPositionTask queryTask = new QueryPositionTask(context, currentDepotId, currentAreaBean.areaId);
                queryTask.execute();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        List<String> positionList = new ArrayList<>();
        currentPositionBean = null;
        if (positionBeanList != null) {
            for (int i = 0; i < positionBeanList.size(); i++) {
                positionList.add(positionBeanList.get(i).positionName);
                currentPositionBean = positionBeanList.get(0);
            }
        }
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.style_spinner, positionList);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_position.setAdapter(adapter1);
        spin_position.setSelection(0, true);
        spin_position.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentPositionBean = positionBeanList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //绑定数据
    private void BindAreaData(final Context context ,String areaId,String positionId) {
        List<String> areaList = new ArrayList<>();
        currentAreaBean = null;
        int currentAreaId = 0;
        if (areaBeanList != null) {
            for (int i = 0; i < areaBeanList.size(); i++) {
                areaList.add(areaBeanList.get(i).areaName);
                if (areaBeanList.get(i).areaId.equals(areaId)) {
                    currentAreaId = i;
                    currentAreaBean = areaBeanList.get(i);
                }
            }
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.style_spinner, areaList);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_area.setAdapter(adapter);
        spin_area.setSelection(currentAreaId, true);
        spin_area.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentAreaBean = areaBeanList.get(position);
                CommonUtil.showPress(context);
                QueryPositionTask queryTask = new QueryPositionTask(context, currentDepotId, currentAreaBean.areaId);
                queryTask.execute();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        List<String> positionList = new ArrayList<>();
        int currentPositionId = 0;
        if (positionBeanList != null) {
            for (int i = 0; i < positionBeanList.size(); i++) {
                positionList.add(positionBeanList.get(i).positionName);
                if (positionBeanList.get(i).positionId.equals(positionId)) {
                    currentPositionId = i;
                    currentPositionBean = positionBeanList.get(i);
                }
            }
        }
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.style_spinner, positionList);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_position.setAdapter(adapter1);
        spin_position.setSelection(currentPositionId, true);
        spin_position.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentPositionBean = positionBeanList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //绑定数据
    private void BindPositionData(Context context) {
        List<String> positionList = new ArrayList<>();
        currentPositionBean = null;
        if (positionBeanList != null) {
            for (int i = 0; i < positionBeanList.size(); i++) {
                positionList.add(positionBeanList.get(i).positionName);
                currentPositionBean = positionBeanList.get(0);
            }
        }
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.style_spinner, positionList);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin_position.setAdapter(adapter1);
        spin_position.setSelection(0, true);
        spin_position.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentPositionBean = positionBeanList.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    //查询库存服务
    class QueryTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        boolean mrefresh = true;
        private List<StockDistributeBean> newData = new ArrayList<>();

        QueryTask(Context context, boolean refresh) {
            super();
            mcontext = context;
            mrefresh = refresh;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                String condition = et_bar.getUpperText().toString();
                //重新刷新
                if (mrefresh) {
                    pageBean = null;
                }
                //第一次程序时需要先查询分页信息
                if (pageBean == null) {
                    pageBean = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_01(condition);
                    currentPage = 0;
                }
                //翻页
                currentPage++;
                //判断是否全部显示，如果没有就查询下一页
                if (currentPage <= pageBean.pageCount) {
                    newData = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_02(condition, pageBean.pageSize, currentPage);
                }
                //超过最大页则返回
                if (currentPage > pageBean.pageCount) {
                    currentPage = pageBean.pageCount;
                }
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext, newData, mrefresh);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //查询库区服务
    class QueryAreaTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        String depotId = "";


        QueryAreaTask(Context context, String depotId) {
            super();
            mcontext = context;
            this.depotId = depotId;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                areaBeanList = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_04(depotId);
                if (areaBeanList.size() > 0) {
                    positionBeanList = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_05(depotId, areaBeanList.get(0).areaId);
                } else {
                    positionBeanList = new ArrayList<>();
                }
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindAreaData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //查询货架服务
    class QueryPositionTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        String depotId = "";
        String areaId = "";

        QueryPositionTask(Context context, String depotId, String areaId) {
            super();
            mcontext = context;
            this.depotId = depotId;
            this.areaId = areaId;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                positionBeanList = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_05(depotId, areaId);
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindPositionData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //region 以下为扫码相关

    //初始化
    private void init() {
        //初始化Profile
        ScanHelper.CreateProfile(this);
        //设置广播消息的接收器
        registerReceiver();
    }

    //扫码处理
    private void ScanData(String barCode) throws Exception {
        if (barCode != null && barCode.length() > 0) {
            if (scanFlag == 0) {
                //扫描配件
                CommonUtil.showPress(this);
                QueryBarCodeTask queryBarCodeTask = new QueryBarCodeTask(StockDistributeActivity.this, barCode);
                queryBarCodeTask.execute();
            } else {
                //扫描货架
                CommonUtil.showPress(this);
                QueryAreaByPositionTask queryAreaByPositionTask = new QueryAreaByPositionTask(StockDistributeActivity.this, currentDepotId, barCode);
                queryAreaByPositionTask.execute();
            }
        }
    }

    //查询条码服务
    class QueryBarCodeTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String barCode;
        private BarCodeBean barCodeBean = null;
        private List<StockDistributeBean> newData = new ArrayList<>();

        QueryBarCodeTask(Context context, String barCode) {
            super();
            mcontext = context;
            this.barCode = barCode;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                barCodeBean = WebServiceHelper.ParseBarCode(barCode);
                if (barCodeBean == null || barCodeBean.barType == BarCodeBean.BAR_UNKNOWN) {
                    msg = "无效二维码：" + barCode;
                    return msg;
                } else if (barCodeBean.partBean != null) {
                    //根据配件查询订单
                    String condition = barCodeBean.partBean.partCode + "|" + barCodeBean.partBean.supplierCode;
                    pageBean = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_01(condition);
                    currentPage = 0;
                    //翻页
                    currentPage++;
                    //判断是否全部显示，如果没有就查询下一页
                    if (currentPage <= pageBean.pageCount) {
                        newData = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_02(condition, pageBean.pageSize, currentPage);
                    }
                    //超过最大页则返回
                    if (currentPage > pageBean.pageCount) {
                        currentPage = pageBean.pageCount;
                    }
                } else if (barCodeBean.shelvesBean != null) {
                    //货架
                    msg = "无效二维码：" + barCode;
                    return msg;
                }
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext, newData, true);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //反向查询货架服务
    class QueryAreaByPositionTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String barCode;
        private String depotId;
        private StockAreaPositionBean stockAreaPositionBean;

        QueryAreaByPositionTask(Context context, String barCode, String depotId) {
            super();
            mcontext = context;
            this.barCode = barCode;
            this.depotId = depotId;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //根据仓库编号和货架二维码，反向查询货位信息
                stockAreaPositionBean = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_06(depotId, barCode);
                if (stockAreaPositionBean != null) {
                    positionBeanList = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_05(depotId, stockAreaPositionBean.areaId);
                    msg = "成功";
                } else {
                    msg = "未查询到货架和库区信息";
                }
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                //设置界面下拉框
                BindAreaData(mcontext, stockAreaPositionBean.areaId, stockAreaPositionBean.positionId);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unRegisterReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
    }

    //注册广播
    private void registerReceiver() {
        //IntentFilter intFilter = new IntentFilter(ScanManager.ACTION_SEND_SCAN_RESULT);
        //registerReceiver(mResultReceiver, intFilter);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScanHelper.ACTION_RESULT_NOTIFICATION);   // for notification result
        filter.addAction(ScanHelper.ACTION_RESULT);                // for error code result
        filter.addCategory(Intent.CATEGORY_DEFAULT);    // needed to get version info
        filter.addAction(ScanHelper.ACTION_SCAN_OUTPUT); // register to received broadcasts via DataWedge scanning
        registerReceiver(mResultReceiver, filter);
    }

    //取消注册广播
    private void unRegisterReceiver() {
        try {
            unregisterReceiver(mResultReceiver);
        } catch (Exception e) {
            Toast.makeText(this, "注销扫码失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    //监听扫码数据的广播，当设置广播输出时作用该方法获取扫码数据
    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            String action = intent.getAction();
            if (action.equals(ScanHelper.ACTION_SCAN_OUTPUT)) {
                // 处理扫码结果
                try {
                    String decodedData = intent.getStringExtra("com.symbol.datawedge.data_string");
                    if (decodedData != null && decodedData.length() > 0) {
                        if (scanFlag == 0) {
                            et_bar.setText(decodedData);
                        }
                        ScanData(decodedData);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(StockDistributeActivity.this, "扫描失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    //适配器相关功能
    class StockAdapter extends CommonAdapter<StockDistributeBean> {
        //构造函数
        public StockAdapter(Context context) {
            super(context);
        }

        @Override
        public void convert(CommonViewHolder holder, final StockDistributeBean stockInfoBean, int position) {
            TextView tv_partNo = holder.getView(R.id.tv_partNo);
            TextView tv_partName = holder.getView(R.id.tv_partName);
            TextView tv_supplierName = holder.getView(R.id.tv_supplierName);
            TextView tv_ck = holder.getView(R.id.tv_ck);
            ImageView iv_img = holder.getView(R.id.iv_img);
            //获取相应索引的ItemBean对象
            StockDistributeBean bean = mList.get(position);
            //设置控件的对应属性值
            tv_partNo.setText(bean.partNo);
            tv_partName.setText(bean.partName);
            tv_supplierName.setText(bean.supplierName);
            tv_ck.setText(bean.depotName);
            if (bean.imgUrl != null && !bean.imgUrl.equals("")) {
                Picasso.with(mContext).load(bean.imgUrl).into(iv_img);
            } else {
                iv_img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_noimage));
            }
            //修改库位
            TextView tv_positiom = holder.getView(R.id.tv_positiom);
            tv_positiom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert_shelves(stockInfoBean);
                }
            });
        }

        //弹出修改货架窗口
        private void alert_shelves(final StockDistributeBean stockDistributeBean) {
            //1.创建一个Dialog对象,去除标题栏
            final Dialog mDialog = new Dialog(mContext);
            mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //2.填充布局,将自定义布局设置进去
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View dialogView = inflater.inflate(R.layout.dialog_partinfo_distribute, null);
            mDialog.setContentView(dialogView);
            //3.设置指定的宽高,如果不设置的话，弹出的对话框可能不会显示全整个布局，当然在布局中写死宽高也可以
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            Window window = mDialog.getWindow();
            lp.copyFrom(window.getAttributes());
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            //注意要在Dialog show之后，再将宽高属性设置进去，才有效果
            mDialog.show();
            window.setAttributes(lp);
            //4、设置点击其它地方不让消失弹窗
            mDialog.setCancelable(false);
            //5、初始化控件
            currentDepotId = stockDistributeBean.depotId;
            TextView tv_depot = dialogView.findViewById(R.id.tv_depot);
            tv_depot.setText(stockDistributeBean.depotName);
            spin_area = dialogView.findViewById(R.id.spin_area);
            spin_position = dialogView.findViewById(R.id.spin_position);

            TextView tv_ok = dialogView.findViewById(R.id.tv_ok);
            tv_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置扫描标志为扫描配件
                    scanFlag = 0;
                    //校验
                    if (currentAreaBean == null) {
                        Toast.makeText(StockDistributeActivity.this, "库区不能为空", Toast.LENGTH_LONG).show();
                        return;
                    }
                    if (currentPositionBean == null) {
                        Toast.makeText(StockDistributeActivity.this, "货架不能为空", Toast.LENGTH_LONG).show();
                        return;
                    }
                    //按下确定键后的事件
                    StockDistributeBean temp = new StockDistributeBean();
                    temp.partId = stockDistributeBean.partId;
                    temp.partName = stockDistributeBean.partName;
                    temp.partCode = stockDistributeBean.partCode;
                    temp.partNo = stockDistributeBean.partNo;
                    temp.supplierId = stockDistributeBean.supplierId;
                    temp.supplierCode = stockDistributeBean.supplierCode;
                    temp.supplierName = stockDistributeBean.supplierName;
                    temp.depotId = stockDistributeBean.depotId;
                    temp.depotName = stockDistributeBean.depotName;
                    temp.positionId = currentPositionBean.positionId;
                    temp.positionName = currentPositionBean.positionName;
                    temp.areaId = currentAreaBean.areaId;
                    temp.areaName = currentAreaBean.areaName;
                    CommonUtil.showPress(StockDistributeActivity.this);
                    PositionTask positionTask = new PositionTask(StockDistributeActivity.this, temp);
                    positionTask.execute();
                    clearLeftMenuList();
                    mDialog.dismiss();
                }
            });
            TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置扫描标志为扫描配件
                    scanFlag = 0;
                    mDialog.dismiss();
                }
            });
            //执行查询
            CommonUtil.showPress(StockDistributeActivity.this);
            QueryAreaTask queryAreaTask = new QueryAreaTask(StockDistributeActivity.this, stockDistributeBean.depotId);
            queryAreaTask.execute();

            //6、扫描标志设置为扫描货架
            scanFlag = 1;
        }
    }

    //修改货架服务
    class PositionTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private StockDistributeBean stockDistributeBean;
        private List<StockDistributeBean> newData = new ArrayList<>();

        PositionTask(Context context, StockDistributeBean stockDistributeBean) {
            super();
            mcontext = context;
            this.stockDistributeBean = stockDistributeBean;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_03(stockDistributeBean);
                if (msg.contains("成功")) {
                    pageBean = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_01("");
                    currentPage = 0;
                    //翻页
                    currentPage++;
                    //判断是否全部显示，如果没有就查询下一页
                    if (currentPage <= pageBean.pageCount) {
                        newData = WebServiceHelper_StockDistribute.STOCKDISTRIBUTE_02("", pageBean.pageSize, currentPage);
                    }
                    //超过最大页则返回
                    if (currentPage > pageBean.pageCount) {
                        currentPage = pageBean.pageCount;
                    }
                }
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            et_bar.setText("");
            if (msg.contains("成功")) {
                //数据
                if (newData.size() > 0) {
                    stockAdapter.clearAddItem(newData);
                } else {
                    stockAdapter.changeMoreStatus(LOADING_END);
                }
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //endregion

}
